package com.gph.saviorframework.config.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gph.saviorframework.Constants;
import com.gph.saviorframework.FieldType;
import com.gph.saviorframework.common.model.SubItem;
import com.gph.saviorframework.config.service.SubItemService;
import com.gph.saviorframework.dao.config.SubItemDao;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

public class SubItemServiceImpl implements SubItemService {

    private Map<String, String> fields = new HashMap<String, String>();

    public SubItemServiceImpl() {
        fields.put("SUB_ITEM_NAME", FieldType.STRING);
    }

    @Autowired
    private SubItemDao subItemDao;

    /**
     * 保存
     *
     * @param subItem
     */
    @Override
    public void save(SubItem subItem) {
        subItemDao.save(subItem);
    }

    /**
     * 根据编号获取
     *
     * @param id
     * @return
     */
    @Override
    public SubItem get(Long id) {
        return subItemDao.get(id);
    }

    /**
     * 更新
     *
     * @param subItem
     * @return
     */
    @Override
    public void update(SubItem subItem) {
        subItemDao.update(subItem);
    }

    /**
     * 批量更新
     *
     * @param subItems
     */
    @Override
    public void update(Set<SubItem> subItems) {
        for (SubItem subItem : subItems) {
            update(subItem);
        }
    }

    /**
     * 根据条件返回数据
     *
     * @param sort
     * @param dir
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> find(String sort, String dir, Map<String, Object> params) {
        return subItemDao.find(sort, dir, genWhereClause(params));
    }

    @Override
    public Map<String, Object> find(int start, int limit, String sort, String dir, Map<String, Object> params, Object... extras) {
        Map<String, Object> map = new HashMap<String, Object>();

        PageHelper.startPage(start, limit);
        List<Map<String, Object>> list = find(sort, dir, params, extras[0].toString());
        PageInfo pageInfo = new PageInfo(list);

        map.put(Constants.DEFAULT_RECORD_MODEL_KEY, list);
        map.put(Constants.DEFAULT_COUNT_MODEL_KEY, pageInfo.getPages());
        map.put(Constants.DEFAULT_RECORDS_MODEL_KEY, pageInfo.getTotal());
        map.put(Constants.DEFAULT_SUCCESS_KEY, Boolean.TRUE);

        return map;
    }

    /**
     * 统计数量
     *
     * @param params
     * @return
     */
    @Override
    public long count(Map<String, Object> params) {
        return 0;
    }

    /**
     * 根据主键删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        subItemDao.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteByIds(Long[] ids) {
        for (long id : ids) {
            delete(id);
        }
    }

    /**
     * 查询字段集合
     *
     * @return
     */
    @Override
    public Map<String, String> getFields() {
        return fields;
    }

    /**
     * 根据id查询map
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findById(Long id) {
        return subItemDao.findById(id);
    }

    /**
     * 生成where条件
     *
     * @param params
     * @param extra
     * @return
     */
    @Override
    public String genWhereClause(Map<String, Object> params, Object... extra) {
        String whereClause = "";
        if (extra.length == 1) {
            whereClause = "1=1 AND SI.ITEM_ID='" + extra[0].toString() + "' ";
        }

        if (params != null && params.keySet().size() > 0) {
            for (String key : params.keySet()) {
                if (fields.get(key).equals(FieldType.STRING)) {
                    whereClause += " and SI." + key + " LIKE '%" + params.get(key) + "%'";
                } else {
                    whereClause += " and SI." + key + " ='" + params.get(key) + "'";
                }
            }
        }

        return whereClause;
    }

    @Override
    public List<SubItem> findEntityByItem(String itemId) {
        return subItemDao.findEntityByItem(itemId);
    }

    @Override
    public List<Map<String, String>> findMapByItem(String itemId) {
        return subItemDao.findMapByItem(itemId);
    }

    @Override
    public List<Map<String, Object>> find(String sort, String dir, Map<String, Object> params, String itemId) {
        return subItemDao.find(sort, dir, genWhereClause(params, itemId));
    }

    @Override
    public long count(String itemId, Map<String, Object> params) {
        return subItemDao.count(genWhereClause(params, itemId));
    }

    @Override
    public Map<String, Map<String, String>> findByItemIds(String... itemIds) {
        Map<String, Map<String, String>> subItemMaps = new HashMap<String, Map<String, String>>();
        List<SubItem> subItems = new ArrayList<SubItem>();

        Map<String, String> subItemMap = null;
        for (String s : itemIds) {
            subItemMap = new HashMap<String, String>();
            for (SubItem subItem : findEntityByItem(s)) {
                String subItemValue = subItem.getValue();
                String subItemName = subItem.getName();
                subItemMap.put(subItemValue, subItemName);
            }
            subItemMaps.put(s, subItemMap);
            subItemMap = null;
        }
        return subItemMaps;

    }
}
